import json
import logging

from django.forms import model_to_dict
from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views import View

from active.models import Active
from comment.comviews import com_views, server_time
from comment.log_code import LogCodes
from user.models import User


class AciveView(View):
    def render(self, message, code, data=None):
        ret = {}
        ret["message"] = message
        ret["err_code"] = code
        ret["data"] = data
        return JsonResponse(ret)
    
    def get(self, request):
        user = request.GET.get("user")
        sync_time = request.GET.get("rsync_time")
        post_time = request.GET.get("post_time")
        ret = {}
        if sync_time is None or post_time is None:
            return self.render("params is not full", LogCodes.SELECT_ERROR)
        user = User.objects.get(id=user)
        if user == None:
            return self.render("params is not full", LogCodes.SELECT_ERROR)
        actives = Active.get_active(user, sync_time, post_time)
        if actives != None:
            message = "success"
            err_code = LogCodes.SELECT_OK
            ret["data"] = [model_to_dict(value) for value in actives]
        else:
            message = "params is error"
            err_code = LogCodes.SELECT_ERROR
        ret["message"] = message
        ret["err_code"] = err_code
        return JsonResponse(ret)
    
    def post(self, request):
        ret = {}
        body = json.loads(request.body)
        id = body.get("id")
        pwnote = body.get("pwnote")
        commands = body.get("commands")
        post_time = body.get("post_time")
        
        if not all([id, pwnote, commands, post_time]):
            message = "params is not full"
            code = LogCodes.SELECT_ERROR
        rows = Active.objects.filter(id=id).create(pwnote=pwnote, commands=commands, remarks=post_time)
        if rows != 0:
            message = "success"
            code = LogCodes.UPDATE_OK
            request["data"] = {"id": id}
        else:
            message = "update is error"
            code = LogCodes.UPDATE_ERROR
        
        ret["message"] = message
        ret["err_code"] = code
        return JsonResponse(ret)
    
    def put(self, request):
        ret = {}
        body = json.loads(request.body)
        id = body.get("id")
        pwnote = body.get("pwnote")
        commands = body.get("commands")
        post_time = body.get("post_time")
        
        if not all([id, pwnote, commands, post_time]):
            message = "params is not full"
            code = LogCodes.SELECT_ERROR
        rows = Active.objects.filter(id=id).update(pwnote=pwnote, commands=commands, remarks=post_time)
        if rows != 0:
            message = "success"
            code = LogCodes.UPDATE_OK
            request["data"] = {"id": id}
        else:
            message = "update is error"
            code = LogCodes.UPDATE_ERROR
        
        ret["message"] = message
        ret["err_code"] = code
        return JsonResponse(ret)


def active_list(params):
    user_id = params.get("id")
    sync_time = params.get("rsync_time")
    post_time = params.get("post_time")
    data = None
    if not all([user_id,sync_time,post_time]):
        return [LogCodes.SELECT_ERROR, "params is not full", None, 0, server_time()]
    try:
        user = User.objects.get(uid=user_id)
    except Exception as e:
        logging.error(e)
        return [LogCodes.SELECT_ERROR, "params is not full", None, 0, server_time()]
    if user is None:
        return [LogCodes.SELECT_ERROR, "params is not full", None, 0, server_time()]
    actives = Active.get_active(user, sync_time, post_time)
    if actives != None:
        message = "success"
        err_code = LogCodes.SELECT_OK
        success = 1
        data = actives
    else:
        message = "params is error"
        err_code = LogCodes.SELECT_ERROR
        success = 0
    return [err_code, message, data, success, server_time()]


def test(requests):
    resp = {
        "data": {
            "username": "test",
        },
        "message": "success",
        "code": 1
    }
    return JsonResponse(resp)


def index(requests):
    return com_views(requests, {
        "list": active_list
    })


"""
A. 下载云端摘要数据。
B. 删除云端标记的要删除的本地数据，下载云端数据。
C. 上传本地未备份数据到云端。
D. 上传本地更新数据到云端。
E. 删除本地标记的要删除的云端数据， 同时删除本地数据。
"""
